package ui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import tree.PageNode;
import tree.PageTree;
import ui.Main;
import ui.TextEditorPane;
import util.Util;

/**
 * Inserts a hyperlink into the current page.
 * @author TKington
 *
 */
public class LinkAction extends AbstractAction {
	/**
	 * Creates a new LinkAction.
	 */
	public LinkAction() {
		putValue(NAME, "Link");
		putValue(SHORT_DESCRIPTION, "Insert Link to Another Page");
		//putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		putValue(SMALL_ICON, Util.getIcon("link.gif")); //$NON-NLS-1$
	}
	
	public void actionPerformed(ActionEvent evt) {
		TextEditorPane textPane = TextEditorPane.instance();
		
		String name = JOptionPane.showInputDialog(Main.frame, "Page to link to?");
		
		PageTree tree = PageTree.instance();
		PageNode page = tree.getPage(name);
		if(page == null) {
			JOptionPane.showMessageDialog(Main.frame, "Page not found");
			return;
		}
		
		textPane.insertLink(page);
	}
}